package pkg

import (
	"bytes"
	"context"
	"encoding/json"
	"github.com/elastic/go-elasticsearch/v7/esapi"
	"log"
	"shop-microservices/basic/inits"
	"strconv"
)

type Data struct {
	Index string
	Body  interface{}
	Id    int
}

// WelcomeSearchToEs 首页搜索添加到es
func (d *Data) WelcomeSearchToEs() {
	data, err := json.Marshal(d.Body)
	if err != nil {
		log.Fatalf("Error marshaling document: %s", err)
	}

	// Set up the request object.
	req := esapi.IndexRequest{
		Index:      d.Index,
		DocumentID: strconv.Itoa(d.Id),
		Body:       bytes.NewReader(data),
		Refresh:    "true",
	}

	// Perform the request with the client.
	res, err := req.Do(context.Background(), inits.Es)
	if err != nil {
		log.Fatalf("Error getting response: %s", err)
	}
	defer res.Body.Close()
}
